﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JsAndSilverLight.Web
{
    public partial class ReadXmlMenu : System.Web.UI.Page
    {

        /// <summary>
        /// 当前菜单中所有的菜单联接项
        /// </summary>
        private List<HistoryEntity> historys = new List<HistoryEntity>();

        protected void Page_Load(object sender, EventArgs e)
        {

            //创建字符串
            CreateTxt();

            return;

            /// <summary>
            /// 得到一个序列化对像
            /// </summary>
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(historys.GetType()); ;


            //写入文件
            System.IO.StringWriter sw = new System.IO.StringWriter();


            string xmlMenu = System.IO.File.ReadAllText(@"E:\ICenter3.0\原型项目\Bocom.ICenter.UI.WPF\bin\Debug\XmlMenu.xml");

            //得到 菜单字符串 转换成 Xml
            System.Xml.Linq.XDocument xdom = System.Xml.Linq.XDocument.Parse(xmlMenu);
            //遍历所有子节点
            EachMenu(xdom.Root.Elements());

            xmlSerializer.Serialize(sw, historys);

            Response.Write(sw.ToString());
            Response.End();

        }

        void CreateTxt()
        {

            System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

            for (int i = 1; i <= 30; i++)
            {

                stringBuilder.AppendLine("case "+i+" :");
                stringBuilder.AppendLine("result = (from p in _Detalis");
                stringBuilder.AppendLine("         let row = p.Attributes.Values.ToList()");
                stringBuilder.AppendLine("         select");
                stringBuilder.AppendLine("      new");
                stringBuilder.AppendLine("      {");

                for (int k = 0; k < i; k++)
                {
                    stringBuilder.AppendLine("          C" + k + "=row[" + k + "].ToString() ,");
                }
                stringBuilder.AppendLine("      }).ToList();");
                stringBuilder.AppendLine(" break;");


            }

            Response.Write(stringBuilder.ToString());
        }



        /// <summary>
        /// 遍历所有子节点
        /// </summary>
        /// <param name="els">原素集合</param>
        void EachMenu(IEnumerable<System.Xml.Linq.XElement> els)
        {
            foreach (var p in els)
            {
                ///子原素
                IEnumerable<System.Xml.Linq.XElement> ChildElements = p.Elements();
                //如果有子原素
                if (ChildElements.Count() > 0)
                {
                    EachMenu(ChildElements);
                }
                //这个没有子原素一般为菜单项
                ///看看所有的属性 中 是不是存在联接属性 如果存在将这个属性添加到菜单项
                if (p.Attributes().Where(attr => attr.Name.LocalName == "Text").Count() > 0)
                {
                    //将当前对像添加到历史记录集合
                    historys.Add(new HistoryEntity()
                    {
                        页面标题 = p.Attributes().First(attr => attr.Name.LocalName == "Text").Value
                    });
                }
            }
        }

    }

    /// <summary>
    /// 用户浏览历始记录实体对像
    /// </summary>
    public class HistoryEntity : System.ComponentModel.INotifyPropertyChanged
    {
        #region 实现 INotifyPropertyChanged
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion



        #region 构造函数

        #endregion

        #region 定义属性



        private string _页面对像图标 = null;

        /// <summary>
        /// 页面对像
        /// </summary>
        public string 页面对像图标
        {
            get { return _页面对像图标; }
            set
            {
                _页面对像图标 = value;

                OnPropertyChanged("页面对像图标");
            }
        }

        private string _页面标题 = "";

        /// <summary>
        /// 页面对像
        /// </summary>
        public string 页面标题
        {
            get { return _页面标题; }
            set
            {
                _页面标题 = value;

                OnPropertyChanged("页面标题");
            }
        }


        /// <summary>
        /// 当前的地址
        /// </summary>
        private string _Uri = null;

        /// <summary>
        /// 页面对像 当前的地址
        /// </summary>
        public string Uri
        {
            get { return _Uri; }
            set
            {
                _Uri = value;

                OnPropertyChanged("Uri");
            }
        }

        #endregion


        #region 定义变量

        #endregion


        #region 公共方法

        #endregion

        #region 子类可见的方法

        #endregion


        #region 依赖项属性

        #endregion


    }

}